import { useEffect, useState } from 'react';

import { Button, Space, Form, Row, Col, Input, Table, DatePicker } from 'antd';
import { PAGINATION_CONFIG } from '@/constants';
import type { QueryChargeOrderListParams, ChargeOrder } from './services';
import { queryChargeList } from './services'
import { useRequest } from 'ahooks';
import type { ColumnsType } from 'antd/es/table/interface';
import dayjs from 'dayjs';
import UserSelect from '@/components/UserSelect';

const ChargeOrderPage: React.FC = () => {
  const [searchForm] = Form.useForm<QueryChargeOrderListParams>()
  const [searchParams, setSearchParams] = useState<QueryChargeOrderListParams>({
    pageNum: 1, pageSize: 10,
    beginTime: dayjs().subtract(31, 'days').format('YYYY-MM-DD 00:00:00'),
    endTime: dayjs().format('YYYY-MM-DD HH:mm:ss'),
  })
  const list = useRequest(() => queryChargeList(searchParams), {
    refreshDeps: [searchParams],
  })

  const onFinish = (formValues: any) => {
    setSearchParams({
      ...searchParams,
      userId: formValues.user?.id,
      telephone: formValues.telephone,
      beginTime: formValues.timeRange ? formValues.timeRange[0]?.format('YYYY-MM-DD HH:mm:ss') : undefined,
      endTime: formValues.timeRange ? formValues.timeRange[1]?.format('YYYY-MM-DD HH:mm:ss') : undefined,
      pageNum: 1,
    })
  }

  const columns: ColumnsType<ChargeOrder> = [
    {
      title: '用户昵称',
      dataIndex: 'nickname',
      width: 120,
      render: val => val || '--',
    },
    {
      title: '用户手机号',
      dataIndex: 'telephone',
      width: 140,
      render: val => val || '--',
    },
    {
      title: '充值金额(元)',
      dataIndex: 'amount',
      width: 130,
    },
    {
      title: '订单号',
      dataIndex: 'orderCode',
      width: 140,
      render: (val, record) => val || record.thirdCode,
    },
    {
      title: '付款时间',
      dataIndex: 'payTime',
      width: 160,
      render: val => val && dayjs(val).format('YYYY-MM-DD HH:mm:ss'),
    },
  ]

  useEffect(() => {
    const refresh = () => {
      list.refresh()
    }
    window.addEventListener('refreshChargeOrderList', refresh)
    return () => {
      window.removeEventListener('refreshChargeOrderList', refresh)
    }
  }, []);
  
  return (
    <div className='container'>
      <div className="card search-card">
        <div className="form-area">
          <Form name='searchChargeOrderForm' form={searchForm} onFinish={onFinish} initialValues={{timeRange: [dayjs().subtract(31, 'day'), dayjs()]}}>
            <Row gutter={[20, 20]}>
              <Col span={6}>
                <Form.Item label='用户' name='user'>
                  <UserSelect allowClear inline />
                </Form.Item>
              </Col>
              <Col span={6}>
                <Form.Item label='用户手机号' name='telephone'>
                  <Input placeholder='请输入' />
                </Form.Item>
              </Col>
              <Col span={12}>
                <Form.Item label='付款时间' name='timeRange'>
                  <DatePicker.RangePicker showTime style={{width: '100%'}} disabledDate={current => current.isAfter(dayjs(), 'day')} />
                </Form.Item>
              </Col>
              <Col span={6}>
                <Space>
                  <Button type='primary' htmlType='submit' loading={list.loading}>筛选</Button>
                  <Button htmlType='reset'>重置</Button>
                </Space>
              </Col>
            </Row>
          </Form>
        </div>
      </div>
      <div className="card content-card">
        <Table<ChargeOrder>
          rowKey='id'
          loading={list.loading}
          dataSource={list.data?.data}
          columns={columns}
          scroll={{y: 700, x: 'max-content'}}
          pagination={{
            ...PAGINATION_CONFIG,
            current: searchParams.pageNum,
            pageSize: searchParams.pageSize,
            total: list.data?.totalCount || 0,
            onChange: (page, pageSize) => {
              setSearchParams({
                ...searchParams,
                pageNum: page === searchParams.pageNum ? 1 : page,
                pageSize,
              })
            }
          }}
        />
      </div>
    </div>
  );
};

export default ChargeOrderPage;
